package org.java.util;





import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : ���
 * @create: 2022/02/11 ���� 9:57
 */
@WebFilter("/*")
public class LoginFilter implements Filter {


    private static List<String> list = new ArrayList<>();

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
                HttpServletRequest httpServletRequest=(HttpServletRequest) req;
                String servletpath=httpServletRequest.getServletPath();
                String method = req.getParameter("method");

//不过滤处理登录界面，js，json，css文件


        if(!servletpath.equals("/page/login.jsp")&&!servletpath.equals("/ajax")&&!servletpath.equals(".js")&&!servletpath.equals(".css")&&!servletpath.equals(".json")&&!servletpath.equals(".jpg")&&!method.contains("init")){

            HttpServletRequest request  = (HttpServletRequest) req;
            HttpSession session = request.getSession();

            if(session.getAttribute("login").equals(0)){
                HttpServletResponse response = (HttpServletResponse) resp;
                response.sendRedirect("/login.jsp");

            }else{
                chain.doFilter(req, resp);
            }
        }else{

            chain.doFilter(req, resp);
        }



    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
